package cn.highedu.coolsharkhub.controller;

import cn.highedu.coolsharkhub.service.UploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传控制层
 * 用于处理上传相关请求
 * @author HighEDU
 */
@RestController
public class UploadController {
    private Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private UploadService uploadService;

    /**
     * 上传文件，请求路径为 /api/upload/images，和前端表单中的action一致
     * consumes = "multipart/form-data" 表示请求体中的数据类型是 multipart/form-data
     * 和前端表单中的enctype一致
     * @param imageFile 文件 必须的参数 需要和前端表单中的name一致
     * @return 文件存储路径
     */
    @PostMapping(value = "/api/upload/images", consumes = "multipart/form-data")
    @ResponseStatus(HttpStatus.CREATED) // 201
    public String uploadImages(MultipartFile imageFile) {
        return uploadService.saveUploadFile(imageFile);
    }
}
